package com.kcwl.idea.plugins.docconverter;

import com.kcwl.idea.plugins.base.AnnotationUtil;
import com.kcwl.idea.plugins.base.JavadocUtil;
import com.kcwl.idea.plugins.base.constants.SwaggerConstants;
import com.kcwl.idea.plugins.base.utils.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

/**
 * swagger注释转javadoc
 */
public class SwaggerToJavaDocAction extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
        int choice = JOptionPane.showConfirmDialog(null, "swagger转javadoc？" , "确认", JOptionPane.YES_NO_OPTION);
        if (choice != JOptionPane.YES_OPTION) {
            return;
        }
        // 获取当前编辑器对象
        Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR);
        if (editor != null) {
            // 获取当前光标位置所在的PsiElement
            PsiElement psiElement = DocUtil.getPsiElementAtCaret(editor, anActionEvent.getProject());
            if (psiElement != null) {
                // 获取包含该PsiElement的PsiClass
                PsiClass psiClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class);
                if (psiClass != null) {
                    // 打印类名
                    try {
                        PsiField[] allFields = psiClass.getFields();
                        WriteCommandAction.runWriteCommandAction(psiClass.getProject(), () -> {
                            PsiJavaFile psiFile = (PsiJavaFile) psiClass.getContainingFile();
                            for (PsiField field : allFields) {
                                PsiAnnotation psiAnnotation = AnnotationUtil.findAnnotation(field, SwaggerConstants.ApiModelProperty);
                                if (psiAnnotation == null) {
                                    continue;
                                }
                                String text = AnnotationUtil.processAnnotation(psiAnnotation);
                                if (StrUtil.isBlank(text)) {
                                    continue;
                                }
                                AnnotationUtil.removeAnnotation(field, SwaggerConstants.ApiModelProperty);
                                JavadocUtil.addJavadoc(field, text);
                            }

                            //类上的注解
                            PsiAnnotation psiAnnotation = AnnotationUtil.findAnnotation(psiClass, SwaggerConstants.ApiModel);
                            String text = AnnotationUtil.processAnnotation(psiAnnotation);

                            AnnotationUtil.removeAnnotation(psiClass, SwaggerConstants.ApiModel);
                            if (!StrUtil.isBlank(text)) {
                                JavadocUtil.addJavadoc(psiClass, text);
                            }
                            JavadocUtil.codeFormat(psiClass.getProject(), psiFile);
                        });
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }

    }

}
